// 注意：Vue 3 中使用 createRouter 和 createWebHistory
import { createRouter, createWebHistory } from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login,
    meta: { isPublic: true }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  }
]

// Vue 3 中创建路由实例的方式
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫保持不变
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.getItem('token')) {
    return next('/')
  }
  next()
})

export default router